<?php


namespace app\api\controller;


use app\api\model\MeetDaibanModel;
use app\api\model\MeetDepartmentModel;
use app\api\model\MeetModel;
use app\api\model\MeetPlanModel;
use app\api\model\MeetWeekModel;
use app\common\controller\Api;

class Plan extends Api
{
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function department()
    {
        $user = $this->auth->getUser();
        $list = MeetDepartmentModel::where("")->field('id,name')->select();
        $list = collection($list)->toArray();
        $meet_id = input('meet_id');
        if(empty($meet_id)){
             $this->error('meet_id必传');
        }
        foreach($list as &$v){
            $map = [
                'user_id' => $user['id'],
                'department_id' => $v['id'],
                'cdate' => date('Y-m-d'),
                'meet_id' => $meet_id,
            ];
            $row = MeetDaibanModel::where($map)->order('id desc')->find();
            $v['content'] = $row?$row['content']:'';
        }
        $this->success('成功',$list);
    }

    public function daiban()
    {
        $user = $this->auth->getUser();
        $input = input();
        $department_id = input('department_id');
        $content = input('content');
        if(empty($department_id) || empty($content)){
             $this->error('内容或部门ID,不能为空');
        }
        $meet_id = input('meet_id');
        if(empty($meet_id)){
            $this->error('meet_id必传');
        }
        $map = [
            'user_id' => $user['id'],
            'department_id' => $department_id,
            'cdate' => date('Y-m-d'),
            'meet_id' => $meet_id,
        ];
        $row = MeetDaibanModel::where($map)->find();
        if($row){
            $model = new MeetDaibanModel();
            $model->allowField(true)->save($input,['id' => $row['id']]);
        }else{
            $input['user_id'] = $user['id'];
            $input['cdate'] = date('Y-m-d');
            $model = new MeetDaibanModel($input);
            $model->allowField(true)->save();
        }
        $this->success('保存成功');
    }

    //交办事项详情
    public function daibanDetail()
    {
        $user = $this->auth->getUser();
        $department_id = input('department_id');
        $meet_id = input('meet_id');
        if(empty($meet_id)){
            $this->error('meet_id必传');
        }
        $map = [
            'user_id' => $user['id'],
            'department_id' => $department_id,
            'cdate' => date('Y-m-d'),
            'meet_id' => $meet_id,
        ];
        $row = MeetDaibanModel::where($map)->find();
        $this->success('成功',$row);
    }

    //删除交办事项
    public function del()
    {
        $user = $this->auth->getUser();
        $department_id = input('department_id');
        $meet_id = input('meet_id');
        if(empty($meet_id)){
            $this->error('meet_id必传');
        }
        $map = [
            'user_id' => $user['id'],
            'department_id' => $department_id,
            'meet_id' => $meet_id,
        ];
        $row = MeetDaibanModel::where($map)->delete();
        $this->success('删除成功',$row);
    }

    public function department2()
    {
        $list = MeetDepartmentModel::where("")->field('id,name')->select();
        $list = collection($list)->toArray();
        $this->success('成功',$list);
    }

    // 添加工作计划
    public function addweekPlan()
    {
        $user = $this->auth->getUser();
        $input = input();
        $department_id = input('department_id');
        $content = input('zhongxin');
        if(empty($department_id) || empty($content)){
            $this->error('内容或部门ID,不能为空');
        }
        if(!empty($input['images'])){
            $input['images'] = htmlspecialchars_decode($input['images']);
        }
        if(!empty($input['files'])){
            $input['files'] = htmlspecialchars_decode($input['files']);
        }
        if(empty($input['date_text'])){
             $this->error('date_text必传');
        }
        $meet_id = input('meet_id');
        if(empty($meet_id)){
            $this->error('meet_id必传');
        }
        $week_num = date("w",strtotime($input['date_text']));
        $input['week_num'] = $week_num;
        $weekarray=array("日","一","二","三","四","五","六");
        if(array_key_exists($week_num,$weekarray)){
            $week_text = '星期'.$weekarray[$week_num];
        }
        if($week_text == '星期日'){
            $week_num = 7;
        }
        $input['week_num'] = $week_num;
        $input['week_text'] = $week_text;
        $map = [
            'meet_id' => $input['meet_id'],
            'department_id' => $input['department_id'],
            'user_id' => $user['id'],
            'date_text' => $input['date_text'],
        ];
        $row = MeetPlanModel::where($map)->find();
        if($row){
            $model = new MeetPlanModel();
            $model->allowField(true)->save($input,['id' => $row['id']]);
        }else{
            $input['user_id'] = $user['id'];
            $model = new MeetPlanModel($input);
            $model->allowField(true)->save();
        }
        $this->success('保存成功');
    }

    public function weekText()
    {
        $input = input();
        $week_num = date("w",strtotime($input['date_text']));
        $input['week_num'] = $week_num;
        $weekarray=array("日","一","二","三","四","五","六");
        if(array_key_exists($week_num,$weekarray)){
            $week_text = '星期'.$weekarray[$week_num];
        } else {
            $week_text = '星期日';
        }
        $data = ['week' => $week_text];
        $this->success('成功',$data);
    }

    //
    public function planDetail()
    {
        $data = MeetPlanModel::get(input('id'));
        $this->success('成功',$data);
    }

    //个人计划列表
    public function plan()
    {
        $input = input();
        if(empty($input['meet_id'])){
            $this->error('会议ID必传');
        }
        $user = $this->auth->getUser();
        $map = [
            'user_id' => $user['id'],
            'meet_id' => input('meet_id'),
        ];
        $list = MeetPlanModel::where($map)->order('week_num asc')->select();
        $list = collection($list)->toArray();
        foreach($list as &$v){
            $v['date_text'] = date('m.d',strtotime($v['date_text'])).' '.$v['week_text'];
            $v['files'] = json_decode($v['files']);
            $v['images'] = json_decode($v['images']);
            $user = \app\common\model\User::get($v['user_id']);
            $v['nickname'] = $user?$user['nickname']:'';
            $v['avatar'] = $user?$user['avatar']:'';
        }
        $this->success('成功',$list);
    }

    public function clonePlan()
    {
        $user = $this->auth->getUser();
        $data = MeetPlanModel::get(input('id'));
        if(empty($data)){
             $this->error('参数错误');
        }
        $data = $data->toArray();
        unset($data['id']);
        unset($data['user_id']);
        unset($data['updatetime']);
        unset($data['createtime']);
        $input = $data;
        $input['user_id'] = $user['id'];
        $model = new MeetPlanModel($input);
        $model->allowField(true)->save();
        $this->success('克隆成功');
    }

    public function delPlan()
    {
        $row = MeetPlanModel::where('id',input('id'))->delete();
        $this->success('删除成功',$row);
    }

    //添加周反馈
    public function addweek()
    {
        $user = $this->auth->getUser();
        $input = input();
        $meet_id = input('meet_id');
        if(empty($meet_id)){
            $this->error('meet_id必传');
        }
        $map = [
            'meet_id' => $input['meet_id'],
            'user_id' => $user['id'],
        ];
        $row = MeetWeekModel::where($map)->find();
        if($row){
            $model = new MeetWeekModel();
            $model->allowField(true)->save($input,['id' => $row['id']]);
        }else{
            $input['user_id'] = $user['id'];
            $model = new MeetWeekModel($input);
            $model->allowField(true)->save();
        }
        $this->success('保存成功','');
    }

    public function weekDetail()
    {
        $user = $this->auth->getUser();
        $meet_id = input('meet_id');
        if(empty($meet_id)){
            $this->error('meet_id必传');
        }
        $input = input();
        $map = [
            'meet_id' => $input['meet_id'],
            'user_id' => $user['id'],
        ];
        $data = MeetWeekModel::where($map)->find();
        $this->success('成功',$data);
    }

    public function weeklist()
    {
        $input = input();
        if(empty($input['meet_id'])){
             $this->error('会议ID必传');
        }
        //$list = MeetDepartmentModel::where("id=3")
        $meet_id = $input['meet_id'];
        $type = input('type');
        if($type == 'last_feedback'){
            //查询一个上周的行政会议
            $meet = MeetModel::where('type',3)->order('id desc')->find();
            if($meet){
                $meet = MeetModel::where('type',3)
                    ->where('id','<',$meet['id'])
                    ->order('id desc')
                    ->find();
                if($meet){
                    $meet_id = $meet['id'];
                }else{
                    $meet_id = '';
                }
            }else{
                $meet_id = '';
            }
        }
        $list = MeetDepartmentModel::where("")
            ->field('id,name')
            ->select();
        $list = collection($list)->toArray();
        foreach($list as &$v){
            $map = [
                'meet_id' => $meet_id,
                'department_id' => $v['id'],
            ];
            $week = MeetWeekModel::where($map)->order('id desc')->find();
            $v['week'] = $week?:'';
        }
        $this->success('成功',$list);
    }

    public function planlists()
    {

        $input = input();
        if(empty($input['meet_id'])){
            $this->error('会议ID必传');
        }
        $meet_id = $input['meet_id'];
        $type = input('type');
        if($type == 'last_plan'){
            //查询一个上周的行政会议
            $meet = MeetModel::where('type',3)->order('id desc')->find();
            if($meet){
                $meet = MeetModel::where('type',3)
                    ->where('id','<',$meet['id'])
                    ->order('id desc')
                    ->find();
                if($meet){
                    $meet_id = $meet['id'];
                }else{
                    $meet_id = '';
                }
            }else{
                $meet_id = '';
            }
        }
        $map = [
            'meet_id' => $meet_id
        ];
        //$map = "id=4";
        $data = MeetPlanModel::where($map)
            ->field('id,meet_id,week_text,week_num,date_text')
            ->group('week_text')
            ->order('week_num asc')
            ->paginate(3)
            ->toArray();
        foreach($data['data'] as &$val){
            $val['date_text'] = date('m.d',strtotime($val['date_text'])).' '.$val['week_text'];
            $where = [
                'meet_id' => $val['meet_id'],
                'week_num' => $val['week_num']
            ];
            $list = MeetPlanModel::where($where)->select();
            foreach($list as &$vv){
                $user = \app\common\model\User::get($vv['user_id']);
                $vv['nickname'] = $user?$user['nickname']:'';
                $vv['avatar'] = $user?$user['avatar']:'';
                $vv['files'] = json_decode($vv['files']);
                $vv['images'] = json_decode($vv['images']);
            }
            $val['list'] = $list;
        }
        $this->success('成功',$data);
    }




}
